/*
 * @(#)UserConfig.java   1.0  2021年11月24日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.loadconfig;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Component;

import com.swift.core.env.AbstractConfigInterface;
import com.swift.core.env.KeyValueConfig;
import com.swift.exception.extend.SystemException;
import com.swift.util.exec.PathUtil;
import com.swift.util.exec.PropertiesUtil;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年11月24日
 */
@Component
public class UserConfig extends AbstractConfigInterface<KeyValueConfig> {

    private static final String user_file = "/data/config/user.properties";
    
    @PostConstruct
    private void init() {
        super.load();
    }
    /** 
     * @see com.swift.core.env.ConfigInterface#loadNew()
     */
    @Override
    public List<KeyValueConfig> loadNew() {
        try {
            InputStream in = new FileInputStream(PathUtil.findOnClassPath(user_file));
            Properties properties = PropertiesUtil.getProperties(in);
            if(properties==null) return null;
            List<KeyValueConfig> list = new LinkedList<KeyValueConfig>();
            for(Object key:properties.keySet()) {
                String keyStr = TypeUtil.toString(key);
                list.add(new KeyValueConfig(keyStr, properties.getProperty(keyStr)));
            }
            return list;
        } catch (FileNotFoundException e) {
            throw new SystemException("读"+user_file+"文件失败",e);
        }
    }

}
